using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 进入或离开区域触发
    /// 参数：1.是否是进入区域时触发，如果填否，则为离开时触发
    /// 注意该触发器必须挂载到包含区域动态参数的实体上时才能生效
    /// </summary>
    public class TrigEnterOrLeaveArea : BaseServiceTrigger
    {
        private bool _isRequireEnter;
        public override void Init(BaseServiceTriggerConfig paramConfig)
        {
            base.Init(paramConfig);
            var config = _paramConfig as TrigParamEnterOrLeaveArea;
            if (config == null)
            {
                LogicLog.LogError($"触发器[{nameof(TrigEnterOrLeaveArea)}]参数错误。");
                return;
            }
            _isRequireEnter = config.isRequireEnter;
        }

#if UNITY_EDITOR
        public override void SetData(LevelService service, BaseLevelFunctionHost host)
        {
            base.SetData(service, host);
            LevelEntity entity = host as LevelEntity;
            if (entity == null)
            {
                LogicLog.LogError($"服务[id={service.config.id}]包含[进入或离开区域]触发器的服务，但它不属于任何实体，将导致该触发器永远无法被触发。");
            }
            else if (!entity.HasDynamicParam(EntityDynamicParamType.Area))
            {
                LogicLog.LogError($"实体[id={entity.entityConfig?.id}]挂载了包含[进入或离开区域]触发器的服务，但它不是区域虚拟实体，将导致该触发器永远无法被触发。");
            }
        }
#endif

        public override bool OnTrigger(LevelPlayer player, object dynamicParam, TrigValueTypeParam dynamicValueTypePram)
        {
            bool isEnteringArea = dynamicValueTypePram.param1 == 1;
            if ((isEnteringArea && _isRequireEnter) || (!isEnteringArea && !_isRequireEnter))
            {
                return base.OnTrigger(player, dynamicParam, dynamicValueTypePram);
            }
            return false;
        }

        public override void OnReset()
        {
            base.OnReset();
            _isRequireEnter = false;
        }
    }
}
